<?php

class Core_Backend_Component_Article_Controller_Category extends Dcmp_Component_Controller {

    public function indexAction($pageindex = 1, $pagesize = 20, $sort = 'id', $dir = 'asc') {
        $this->getHistory()->record();
        $validator = $this->getRequest()->getValidator('filter');
        $collection = App::getModel('article/collection_category');
        $dir == 'asc' ? $collection->asc($sort) : $collection->desc($sort);
        if ($validator->isNotEmpty('title')) {
            $collection->like('title', ':title:s')
            		   ->addParameter('title', '%' . $validator->getValue('title') . '%');
        }
        $collection->joinTemplate()->joinAccount()->pagination($pageindex, $pagesize)->load();
        $records = $collection->loadCount();
        return $this->viewResult(array('list' => $collection, 'records' => $records));
    }

    public function createAction() {
        $categories = App::getModel('article/collection_category')->load();
        $templates = App::getModel('design/collection_page_template')->load();
        $themes = App::getModel('design/collection_page_theme')
                        ->filterByType('article')
                        ->filterByEnabled(1)
                        ->load();
        return $this->viewResult(array('parent_categories' => $categories, 'templates'=>$templates, 'themes'=>$themes));
    }

    public function editAction($id) {
        $model = App::getModel('article/category')->load($id);
        $result = $this->createAction();
        $result->getParentCategories()->removeItemById($model->getId());
        $this->getPage()->getNavigation()->addLabel($model->getTitle());
        return $result->setModel($model);
    }  

    public function sortAction($parent = 0) {
        if ($parent == 0) {
            $categories = App::getModel('article/collection_category')
            					->eq('parent_id', 0)
            					->load();
        } else {
            $categories = App::getModel('article/category')
            					->setId($parent)
            					->getChildren();
        }
        return $this->viewResult(array('categories' => $categories));
    }

    public function sortPostAction() {
        $sorting = $this->getRequest()->getForm('sorting');
        $table = App::getModel('article/category')->getTable();
        foreach ($sorting as $index => $id) {
            $table->updateSorting($id, $index + 1);
        }
        return $this->redirectResult($this->getHistoryUrl('article/category/index'));
    }

    public function savePostAction() {
        $data = $this->getRequest()->getForm('category');

        $model = App::getModel('article/category');
        if (isset($data['id']) && !empty($data['id'])) {
            $model->load($data['id']);
        }

        $model->addData($data)->setCreatorId(Kd_Web::getIdentity()->getId());
        if (($errors = $model->validate() === TRUE)) {
            $model->save();
            $this->getMessager()->addInform($this->__('share.message.success'));
            return $this->redirectResult($this->getHistoryUrl('article/category/index'));
        } else {
            $this->getMessager()->addError($this->__('share.message.fail'))
                    ->addError($errors);
            return $this->createAction()->setModel($model);
        }
    }
}